<?php

/**
 * MyHammer IndexedMemcache
 *
 * LICENSE
 *
 * This source file is subject to the GNU General Public License Version 3
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.myhammer.de/opensource/license/gpl.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to opensource@myhammer.de so we can send you a copy immediately.
 *
 * @category   MyHammer
 * @package    IndexedMemcache
 * @subpackage utils
 * @license    http://www.myhammer.de/opensource/license/gpl.txt GNU General Public License Version 3
 * @version    1.0
 * @author     Steven Berg <berg@myhammer.de>
 */

class cVardump {

    /**
     * dump Variable
     *
     * @param mixed $mVar
     * @param string $sInfo
     */
    public static function dump($mVar, $sInfo = FALSE) {
        echo self::createOutputContainerTop();
        echo self::createOutputInfo($sInfo);
        echo self::createOutputFile();

        $sVariableName = self::createVariableName();
        self::do_dump($mVar, $sVariableName);

        echo self::createOutputContainerBottom();
    }

    /**
     * get Container Top
     *
     * @return string
     */
    private static function createOutputContainerTop() {
        return '<pre style="margin: 0px 0px 10px 0px; display: block; background: white; color: black; font-family: Verdana; border: 1px solid #cccccc; padding: 5px; font-size: 10px; line-height: 13px;">';
    }

    /**
     * get Container Bottom
     *
     * @return string
     */
    private static function createOutputContainerBottom() {
        return '</pre>';
    }

    /**
     * display Info / DEscription
     *
     * @param string $sInfo
     * @return string
     */
    private static function createOutputInfo($sInfo) {
        if($sInfo) {
            return '<b style="color: red;">' . $sInfo . ':</b><br>';
        }
        return '';
    }

    /**
     * get File
     *
     * @return string
     */
    private static function createOutputFile() {
        $aStack = debug_backtrace();
        $aCaller = $aStack[2];
        $aFileinfo = $aStack[1];

        return 'in <b style="color: blue;">' . $aFileinfo['file'] . $aCaller['class'] . $aCaller['type'] . $aCaller['function'] . '</b> on line '. $aFileinfo['line'] . ': ';
    }

    /**
     * create VaribaleName
     *
     * @return string
     */
    private static function createVariableName() {
        $aStack = debug_backtrace();
        $aCaller = $aStack[2];
        $aFileinfo = $aStack[1];

        $code = explode("\n",preg_replace('/\r\n|\r/', "\n", file_get_contents($aFileinfo['file'])));
        return '<b style="color: red; font-size: 100%;">' . htmlspecialchars(preg_replace('/(.*?)dump\((.*?)\);(.*)/i','$2',$code[$aFileinfo['line']-1])). '</b>';
    }

    /**
     * do dump
     *
     * @param array $aVar
     * @param string $sVariableName
     * @param string $sIndent
     * @param string $sReference
     */
    private static function do_dump(&$aVar, $sVariableName = NULL, $sIndent = NULL, $sReference = NULL) {
        echo self::getType($sIndent, $sVariableName, $aVar);

        $sDoDumpIndent = '<span style="color:#eeeeee;">|</span> &nbsp;&nbsp; ';
        self::getDetails($sIndent, $sDoDumpIndent, $sVariableName, $aVar);
    }

    /**
     * get Details
     *
     * @param string $sIndent
     * @param string $sDoDumpIndent
     * @param string $sVariableName
     * @param array $aVar
     */
    private static function getDetails($sIndent, $sDoDumpIndent, $sVariableName, $aVar) {
        if(is_array($aVar)) {
            $count = count($aVar);
            if ($count > 0) {
                echo  $sIndent . '(<br>';
                $keys = array_keys($aVar);
                foreach($keys as $name) {
                    $value = &$aVar[$name];
                    self::do_dump($value, "['$name']", $sIndent.$sDoDumpIndent, $sReference);
                }
                echo  $sIndent . ')<br>';
            }
        }
    }

    /**
     * get Type of Variable
     *
     * @param string $sIndent
     * @param string $sVariableName
     * @param array $aVar
     * @return string
     */
    private static function getType($sIndent, $sVariableName, $aVar) {
        $sType = strtolower(gettype($aVar));

        switch ($sType) {
            case 'string':
                $type_color = '<span style="color:green">';
                return $sIndent . $sVariableName . ' = <span style="color:#a2a2a2">' . $sType . '(' . strlen($aVar) . ') </span> ' . $type_color . '"' . $aVar . '"</span><br>';
                break;
            case 'integer':
                $type_color = '<span style="color:red">';
                return $sIndent . $sVariableName . ' = <span style="color:#a2a2a2">' . $sType . ' </span>' . $type_color . $aVar . '</span><br>';
                break;
            case 'double':
                $type_color = '<span style="color:#0099c5">';
                return $sIndent . $sVariableName . ' = <span style="color:#a2a2a2">' . $sType . ' </span>' . $type_color . $aVar . '</span><br>';
                break;
            case 'boolean':
                $type_color = '<span style="color:#92008d">';
                return $sIndent . $sVariableName . ' = <span style="color:#a2a2a2">' . $sType . ' </span>' . $type_color . ($aVar == 1 ? 'TRUE':'FALSE') . '</span><br>';
                break;
            case 'null':
                $type_color = '<span style="color:black">';
                return $sIndent . $sVariableName . ' = <span style="color:#a2a2a2">' . $sType . ' </span>' . $type_color . $aVar . '</span><br>';
                break;
            case 'object':
                $type_color = '<span style="color:black">';
                return $sIndent . $sVariableName . ' = <span style="color:#a2a2a2">' . $sType . ' </span>' . $type_color . print_r($aVar). '</span><br>';
                break;
            default:
                $type_color = '<span style="color:black">';
                return $sIndent . $sVariableName . ' = <span style="color:#a2a2a2">' . $sType . '(' .strlen($aVar). ') </span>' . $aVar . '<br><br>';
                break;
        }
    }
}